home *** CD-ROM | disk | FTP | other *** search
- /* ***************************************************************************
- * *
- * This program is to illustrate the use of 2D and Multiple 1D filters *
- * We use here DFIR2D and DFIRM1D modules from LIBCONV *
- * *
- ******************************************************************************
- * *
- * The 1D filter in the X direction will be (1/2,-1.,1/2) *
- * --- Approximation of second derivative *
- * In the Y direction the 1D filter will be (-1/2,0.,1/2) *
- * --- Approximation of first derivative *
- * The equivalent 2D filter will be: *
- * -1/4 0 +1/4 *
- * +1/2 0 -1/2 *
- * -1/4 0 +1/4 *
- * ------ Y -----> *
- * This is the approximation of the triple derivative D()/Dxxy *
- * *
- *************************************************************************** */
- #include <stdio.h>
- #include <math.h>
- #include "conv.h"
- void print_2D_Array( double *input, int nx, int ny);
-
- #define HX 5
- #define HY 5
- #define NX (2*HX+1)
- #define NY (2*HY+1)
-
- #define SCALE_X (M_PI/(double)HX)
- #define SCALE_Y (M_PI/(double)HY)
- #define ABS(a) (((a) > 0) ? (a) : (-(a)))
- double fil2d[9]={ -.25,0.5,-.25,
- 0.0,0.0,0.0,
- .25,-.5,.25};
- double filx[3]={ .5, -1., .5};
- double fily[3]={ -.5, 0.0, 0.5};
- main() {
- double input[NX*NY], out1[NX*NY], out2[NX*NY], temp[NX*NY];
- int i, j;
- for( j = -HY ; j <= HY ; j++) {
- for( i = -HX ; i <= HX ; i++) {
- input[(j+HY)*NX+(i+HX)] =
- cos((double)i*SCALE_X)*(1.-((double)ABS(i))/(double)HX) *
- cos((double)j*SCALE_Y)*(1.-((double)ABS(j))/(double)HY);
- }
- }
- /* 2D FIR filter: */
- dfir2d( input,1,NX,-HX,NX,-HY,NY, fil2d,1,3, -1, 3, -1, 3,
- out1, 1,NX,-HX,NX,-HY,NY, 1.0, 0.0);
-
- /* Multiple 1D FIR filter in the X direction: */
- dfirm1d( input,1,NX,-HX,NX, NY, filx, 1, -1, 3,
- temp, 1,NX,-HX,NX, 1.0, 0.0);
-
- /* Multiple 1D FIR filter in the Y direction: */
- dfirm1d( temp,NX,1,-HY,NY, NX, fily, 1, -1, 3,
- out2,NX,1,-HY,NY, 1.0, 0.0);
-
- /* Print out the input and the two results */
- printf("\n Input : \n");
- print_2D_Array( input, NX, NY);
- printf("\n After application of 2D FIR filter: \n");
- print_2D_Array( out1, NX, NY);
- printf("\n After multiple 1D FIR filters in the X Direction: \n");
- print_2D_Array( temp, NX, NY);
- printf("\n After final multiple 1D FIR filters in the Y Direction: \n");
- print_2D_Array( out2, NX, NY);
- }
- void print_2D_Array( double *ptr, int nx, int ny){
- int i,j;
- double *this;
- for( j=nx ; j > 0; j--, ptr ++) {
- this = ptr;
- for( i=ny ; i>0 ; i--, this += nx)
- printf(" %6.3f",*this);
- printf("\n");
- }
- }
-